<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Request;
use app\atxunda\model\SubQuestion;

class Sub extends Common
{
	public function question(){
		$request = Request::instance();
		$param = $request->param();
		$data = SubQuestion::getAll($param);
		$where = $typeData = $sData = $courseData = [];
		$navData = Db::table('nav')->field('nav_id,nav_name')->where('is_show',1)->order('nav_sort desc,nav_id asc')->select();
		if($param['nav_id']) $where['nav_id'] = $param['nav_id'];
		if($param['nav_id']) $typeData = Db::table('type')->field('type_id,type_name')->where($where)->where('is_show',1)->order('type_sort desc,type_id asc')->select();
		if($param['nav_id'] && $param['type_id']) $where['type_id'] = $param['type_id'];
		if($param['type_id']) $sData = Db::table('specific')->field('s_id,s_name')->where($where)->where('s_show',1)->order('s_sort desc,s_id asc')->select();
		if($param['s_id']) $where['s_id'] = $param['s_id'];
		if($param['nav_id'] && $param['type_id'] && $param['s_id']) $courseData = Db::table('course')->field('course_id,course_name')->where($where)->order('course_sort desc,course_id asc')->select();
		return $this->fetch('',[
			'data' => $data,
			'param' => $param,
			'sData' => $sData,
			'navData' => $navData,
			'typeData' => $typeData,
			'courseData' => $courseData,
		]);
	}

	public function ques_edit(){
		$admin = $this->admin;
		$request = Request::instance();
		$param = $request->param();
		$data = SubQuestion::editDispose($param['id']);
		if(input('post.')){
			$post = input('post.');
			$arr['updated_admin_id'] = $admin['admin_id'];
			$arr['updated_time'] = date("Y-m-d H:i:s");
			$arr = SubQuestion::dispose($post,$admin['admin_id']);
			if($param['id']){
				Db::table('sub_question')->where('id',$param['id'])->update($arr);
			}else{
				$arr['created_admin_id'] = $arr['updated_admin_id'];
				$arr['created_time'] = $arr['updated_time'];
				Db::table('sub_question')->insert($arr);
			}
			$this->success('编辑成功','question','',1);
		}else{
			$type_data = $s_data = $course_data = [];
			$nav_data = Db::table('nav')->field('nav_id,nav_name')->where('is_show',1)->order('nav_sort desc')->select();
			if($data){
				$type_data = Db::table('type')->field('type_id,type_name')->where('nav_id',$data['nav_id'])->where('is_show',1)->order('type_sort desc')->select();
				$s_data = Db::table('specific')->field('s_id,s_name')->where('type_id',$data['type_id'])->where('s_show',1)->order('s_sort desc')->select();
				$course_data = Db::table('course')->field('course_id,course_name')->where('s_id',$data['s_id'])->order('course_sort desc,course_id asc')->select();
			}
			return $this->fetch('',[
				'data' => $data,
				'nav_data' => $nav_data,
				'type_data' => $type_data,
				's_data' => $s_data,
				'course_data' => $course_data,
			]);
		}
	}

	public function ques_del(){
		$request = Request::instance();
		$param = $request->param();
		Db::table('sub_question')->where('id',$param['id'])->delete();
		$this->success('删除成功','question','',1);
	}

	/**
	 * [getType 获取到课程阶段]
	 * @return [type] [description]
	 */
	public function getType(){
		$request = Request::instance();
		$param = $request->param();
		$data = Db::table('type')->field('type_id,type_name')->where('nav_id',$param['nav_id'])->where('is_show',1)->order('type_sort desc,type_id asc')->select();
		return json($data);
	}

	/**
	 * [getS 获取到课程章节]
	 * @return [type] [description]
	 */
	public function getS(){
		$request = Request::instance();
		$param = $request->param();
		$data = Db::table('specific')->field('s_id,s_name')->where('type_id',$param['type_id'])->where('s_show',1)->order('s_sort desc,s_id asc')->select();
		return json($data);
	}

	/**
	 * [getS 获取到课程视频]
	 * @return [type] [description]
	 */
	public function getCourse(){
		$request = Request::instance();
		$param = $request->param();
		$data = Db::table('course')->field('course_id,course_name')->where('s_id',$param['s_id'])->order('course_sort desc,course_id asc')->select();
		return json($data);
	}
}